from data_visualization.html_data_visualization import HtmlDataVisualization


class SimpleChart(HtmlDataVisualization):
    chart_title = ""
    xAxis_name = ""
    xAxis_data = []
    yAxis_name = ""
    yAxis_data = []
    chart_type = ""

    def __init__(self):
        HtmlDataVisualization.__init__(self)
        self.algorithm_name = "简单图表"
        self.html_template_name = "simple_chart-template.html"
        
    def setXAxisAndYAxisName(self, xAxis_name, yAxis_name):
        self.xAxis_name = xAxis_name
        self.yAxis_name = yAxis_name
        self.xAxis_data = self.data_source[self.xAxis_name].values.tolist()
        self.yAxis_data = self.data_source[self.yAxis_name].values.tolist()
    
    def prepareHtmlItems(self):
        HtmlDataVisualization.prepareHtmlItems(self)
        self.html_items["#chart_title#"] = self.chart_title
        self.html_items["#xAxis_name#"] = self.xAxis_name
        self.html_items["#xAxis_data#"] = self.xAxis_data
        self.html_items["#yAxis_name#"] = self.yAxis_name
        self.html_items["#yAxis_data#"] = self.yAxis_data
        self.html_items["#chart_type#"] = self.chart_type
        if self.chart_type == "pie":
            self.html_template_name = "simple_chart_pie-template.html"
            pie_data = []
            for i in range(0, len(self.xAxis_data)):
                pie_data.append({"name":self.xAxis_data[i], "value":self.yAxis_data[i]})
            self.html_items["#pie_data#"] = pie_data

    def generateHtmlFile(self):
        HtmlDataVisualization.generateHtmlFile(self)
        if self.chart_type == "smooth_line" or self.chart_type == "area_line":
            if self.chart_type == "smooth_line":
                self.html_text = self.html_text.replace("type : 'smooth_line',", "type : 'line',")
                self.html_text = self.html_text.replace("// smooth: true,", "smooth: true,")
            
            if self.chart_type == "area_line":
                self.html_text = self.html_text.replace("type : 'area_line',", "type : 'line',")
                self.html_text = self.html_text.replace("// areaStyle: {normal: {}},", "areaStyle: {normal: {}},")
                
            with open(self.html_dest_path, "wt", encoding="UTF-8") as f2:
                f2.write(self.html_text)
